const express = require('express');
const router = express.Router();
const multer = require('multer');
const path = require('path');
const { verifyToken } = require('../middlewares/auth');

// 设置图片存储目录
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, path.join(__dirname, '../public/uploads'));
  },
  filename: function (req, file, cb) {
    const ext = path.extname(file.originalname);
    cb(null, Date.now() + ext);
  }
});
const upload = multer({ storage: storage });

router.post('/', verifyToken, upload.single('image'), (req, res) => {
  if (!req.file) {
    return res.json({ success: false, message: '未上传文件' });
  }
  // 假设静态资源/public已映射
  const url = `/uploads/${req.file.filename}`;
  res.json({ success: true, url });
});

module.exports = router; 